package com.fengmi.service.impl;

import cn.hutool.json.JSONUtil;
import com.fengmi.api.goods.GoodsApi;
import com.fengmi.entity.cart.Cart;
import com.fengmi.entity.goods.MallGoodsSku;
import com.fengmi.entity.sso.SysUser;
import com.fengmi.entity.vo.ResultVO;
import com.fengmi.interceptor.UserInfoInterceptor;
import com.fengmi.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CartServiceImpl implements ICartService {
    @Autowired
    private GoodsApi goodsApi;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public ResultVO addCart(String skuId, Integer buyNum) {

        MallGoodsSku sku = goodsApi.findSkuById(skuId);
        if(sku==null){
            return new ResultVO(false,"商品不存在");
        }
        if (buyNum>sku.getSkuStock()){
            return new ResultVO(false,"库存不足");
        }
        String userId = UserInfoInterceptor.getUserInfo().getUserId();
        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps("menhu:cart");
        Object cartListJson = operations.get(userId);
        if(StringUtils.isEmpty(cartListJson)){
            //购物车信息
            Cart cart = new Cart();
            cart.setSkuId(sku.getSkuId()+"");
            cart.setTitle(sku.getSkuTitle());
            cart.setImg(sku.getSkuImage());
            cart.setSpec(sku.getSkuSpec());
            cart.setPrice(sku.getSkuPrice().doubleValue());
            cart.setMarketPrice(sku.getSkuMarketPrice().doubleValue());
            cart.setBuyNum(buyNum);

            List<Cart> cartList = Arrays.asList(cart);
            operations.put(userId, JSONUtil.toJsonStr(cartList));

            return new ResultVO(true,"成功加入购物车");
        }
        List<Cart> carts = JSONUtil.toList((String) cartListJson, Cart.class);

        Long collect = carts.stream().filter(cart -> skuId.equals(cart.getSkuId())).collect(Collectors.counting());

        if(collect>=1){
            carts.forEach(cart -> {
                if(skuId.equals(cart.getSkuId())){
                    cart.setBuyNum(cart.getBuyNum()+buyNum);
                }
            });
        }else{
            //购物车信息
            Cart cart = new Cart();
            cart.setBuyNum(buyNum);
            cart.setImg(sku.getSkuImage());
            cart.setMarketPrice(sku.getSkuMarketPrice().doubleValue());
            cart.setPrice(sku.getSkuPrice().doubleValue());
            cart.setSkuId(sku.getSkuId()+"");
            cart.setTitle(sku.getSkuTitle());
            cart.setSpec(sku.getSkuSpec());
            carts.add(cart);
        }

        operations.put(userId, JSONUtil.toJsonStr(carts));
        return new ResultVO(true, "加购成功");
    }

    @Override
    public List<Cart> findCartList() {
        SysUser userInfo = UserInfoInterceptor.getUserInfo();
        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps("menhu:cart");
        Object CartListJson = operations.get(userInfo.getUserId());
        return JSONUtil.toList((String) CartListJson,Cart.class);
    }

    @Override
    public ResultVO delete(String skuId) {
        SysUser userInfo = UserInfoInterceptor.getUserInfo();

        //获取用户的购物车列表
        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps("menhu:cart");
        Object cartListJson = operations.get(userInfo.getUserId());
        if(!StringUtils.isEmpty(cartListJson)){
            List<Cart> carts = JSONUtil.toList((String) cartListJson, Cart.class);
            List<Cart> collect = carts.stream().filter(cart ->!cart.getSkuId().equals(skuId)).collect(Collectors.toList());
            //同步到redis中
            operations.put(userInfo.getUserId(),JSONUtil.toJsonStr(collect));
        }
        return new ResultVO(true,"删除成功");
    }

    @Override
    public ResultVO jieSuan(List<Cart> cartList) {
        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps("menhu:cart");
        operations.put(UserInfoInterceptor.getUserInfo().getUserId(),JSONUtil.toJsonStr(cartList));

        return new ResultVO(true,"结算成功");
    }
}
